package com.huanqiu.mylib.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import androidx.core.content.ContextCompat;

public final class PermissionUtil {

    private static final String TAG = "PermissionUtil";

    public static boolean checkPermission(Context ctx, String permission) {
        return ContextCompat.checkSelfPermission(ctx, permission) == PackageManager.PERMISSION_GRANTED;
    }

    public static boolean checkPermissions(Context ctx, String[] permissions) {
        for (String p : permissions) {
            if (ContextCompat.checkSelfPermission(ctx, p) != PackageManager.PERMISSION_GRANTED)
                return false;
        }
        return true;
    }

    /**
     * 跳转到权限管理页面
     *
     * https://blog.csdn.net/donkor_/article/details/79374442
     */
    public static void gotoPermissionManager(Context context) {
        String name = Build.MANUFACTURER.toLowerCase();
        Log.d(TAG, "gotoPermissionManager(): name = " + name);
        switch (name) {
            case "huawei":
            case "xiaomi":
            case "oppo":
            case "vivo":
            default:
                _gotoAppDetailsSettings(context);
                break;
        }
    }

    private static void _gotoAppDetailsSettings(Context context) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.fromParts("package", context.getPackageName(), null));
        try {
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
